export const formatDuration = (second: number) => {
    let duration: string = second + '秒'
    let days = Math.floor(second / 86400)
    let hours = Math.floor((second % 86400) / 3600)
    let minutes = Math.floor(((second % 86400) % 3600) / 60)
    let seconds = Math.floor(((second % 86400) % 3600) % 60)
    if (days > 0) {
        duration = days + '天' + hours + '小时' + minutes + '分' + seconds + '秒'
    } else if (hours > 0) {
        duration = hours + '小时' + minutes + '分' + seconds + '秒'
    } else if (minutes > 0) {
        duration = minutes + '分' + seconds + '秒'
    } else if (seconds > 0) {
        duration = seconds + '秒'
    }
    return duration
}


// 对象数据根据某个字段分组
export const arrayGroupBy = (arr: any, group: string = 'group') => {
    const map: { [key: string]: any } = {}

    for (let i = 0; i < arr.length; i++) {
        const ai = arr[i]
        const mapKey = ai[group] || ''
        if (!map[mapKey]) {
            map[mapKey] = [ai]
        } else {
            map[mapKey].push(ai)
        }
    }

    let res: any = []
    Object.keys(map).forEach(key => {
        res.push({
            group: key,
            data: map[key]
        })
    })

    return res
}

// 获取本地图片资源url
export const requireImgUrl = (name: string) => {
    return new URL(`../../assets/images/dashboard/${name}.png`, import.meta.url).href
}
